import { Controller, Get, Req } from '@nestjs/common';
import { IpService } from './ip.service';
import type { Request } from 'express';
import { resFormatMethod } from '../../utils/res-format';
import { Public } from '../../common/decorators/public.decorator';

@Controller('ip')
export class IpController {
  constructor(private readonly ipService: IpService) {}

  @Public()
  @Get('current')
  getCurrentIp(@Req() req: Request) {
    const ip = this.ipService.getClientIp(req);
    return resFormatMethod(0, '获取IP地址成功', { ip });
  }

  @Public()
  @Get('public')
  async getPublicIp() {
    try {
      const publicIp = await this.ipService.getPublicIp();
      return resFormatMethod(0, '获取公网IP成功', { ip: publicIp });
    } catch (error) {
      return resFormatMethod(1, error.message || '获取公网IP失败', null);
    }
  }
}
